import java.util.Random;

public class Main1 {
    public static void main(String[] args) {
        int[] nums = new int[]{4,7,1,2,67,8};
        quickSort(nums,0,nums.length - 1);
        System.out.println();;
    }

    // 随机快速排序
    private static void quickSort(int[] nums, int left, int right) {
        // 边界处理
        if (left >= right) {
            return;
        }
        // 范围 [left,l] [l + 1, r - 1] [r,right]
        int key = nums[new Random().nextInt(right - left + 1) + left];
        int l = left - 1;
        int r = right + 1;
        int i = left;
        while (i < r) {
            if (nums[i] < key) {
                swap(nums,++l,i++);
            } else if (nums[i] > key) {
                swap(nums,--r,i);
            } else {
                i++;
            }
        }
        quickSort(nums,left,l);
        quickSort(nums,r,right);
    }

    private static void swap(int[] nums, int a, int b) {
        int tmp = nums[a];
        nums[a] = nums[b];
        nums[b] = tmp;
    }
}
